'''
	This file uses the links stored in 'historyLinks.txt' to extract the revision numbers associated with each revision. Each revision in Wikipedia has a unique number associated with it. It also extracts the usernames of the editors who contribute to the corresponding revision. 
	The database is first used in this file. It creates two tables - userRev & Revisions
	'userRev' contains the Revisions at which a new member first started editing
	'Revisions' contains all the revisions of the current Wikipedia Page.
'''




import connect as con
from lxml import html
import re
import database as db
import readLinks as rdL

#This is to check if the given user is registered or anonymous. This can be done by checking the username for an ip address
def isIp(user):
	splitStr = user.split('.')
	if( len(splitStr) == 4 ):
		return True;
	else:
		return False

#This function reverses to the list. This is required as we read the page top to bottom while the revisions are bottom to top
def reverseList( oldList ):
	num = len(oldList)
	newList = []
	i = num-1
	while i >= 0:
		newList.append(oldList[i])
		i -= 1
	return newList

#This sets the correct absolute revision number
def revRevNum( revList ):
	num = len(revList)
	maxRev = revList[0][2] + revList[num-1][2]
	for item in revList:
		item[2] = maxRev - item[2]
	return revList
	
		
def findUserAndRevision(tree, startNum):

	absNum = startNum
	usersRevisions = []
	oldId = ""
	idList = []
	
	for element in tree.iterlinks():
		link = element[2]
		
		# Checking for User
		matchObj = re.match(r'/wiki/User_talk:.*', link)
		if matchObj:
			splitStr = matchObj.group().split(':');
			user = splitStr[1]
			if isIp(user) is False:
				usersRevisions.append([user, oldId, absNum])
		
		#Finding the link to revision		
		splitLink = link.split('?')
		if len(splitLink) > 1 :
			oldid = splitLink[1].split('&')
			if( len(oldid) == 2 ):
				matchObj = re.match(r'oldid=.*', oldid[1] )
				if matchObj:
					absNum += 1
					oldId = oldid[1].split('=')[1]
					idList.append(oldId)
					
					
	idList = reverseList(idList)
	usersRevisions = reverseList(usersRevisions)
	usersRevisions = revRevNum(usersRevisions)
	return usersRevisions, idList, absNum
	
	
def addUsrRevToDb(usrRev):
	conn = db.connectDb()
	db.createTbUsersRev(conn)
	for item in usrRev:
		db.insertTbUsersRev(conn, item)
	db.closeConn(conn)
	
def addRevToDb(idList):
	conn = db.connectDb()
	db.createTbRev(conn)
	for item in idList:
		db.insertTbRev(conn, item)
	db.closeConn(conn)
	
def main():
	links = rdL.readLinksRevPages()
	num = 0
	for link in links:
		url = link.strip('\n');
		tree = con.connectToPage(url)	
		finalList, idList, num = findUserAndRevision(tree, num)
		addUsrRevToDb(finalList)
		addRevToDb(idList)
	exit()
	for item in finalList:
		print item
		
main()
